package com.vip.xchat.service.impl;

import com.vip.file.model.response.ErrorCode;
import com.vip.file.model.response.Result;
import com.vip.file.model.response.Results;
import com.vip.xchat.mapper.UserMapper;
import com.vip.xchat.model.Xuser;
import com.vip.xchat.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<Object> addUser(Xuser xuser) {
        int insertSum = userMapper.insert(xuser);
        if (insertSum > 0) {
            return Results.newSuccessResult(null, "信息发送成功!");
        }
        return Results.newFailResult(ErrorCode.SYSTEM_ERROR, "请求出现问题!");
    }

    @Override
    public Result<Object> checkuser(Xuser xuser) {
        Map<String,Object> serachMap = new HashMap<>();
        serachMap.put("account", xuser.getAccount());
        serachMap.put("password", xuser.getPassword());
        List<Xuser> xusers = userMapper.selectByMap(serachMap);
        if (xusers != null && xusers.size() == 1){
            xusers.get(0).setPassword("");
            return Results.newSuccessResult(xusers.get(0),"登录成功!");
        }
        return Results.newFailResult(ErrorCode.DATA_NONE,"账号或密码不正确!");
    }

    @Override
    public Result<Object> getuserlist(Xuser xuser) {
        List<Xuser> xusers = userMapper.selectByMap(null);
        xusers.forEach(x -> {x.setPassword("");});
        return Results.newSuccessResult(xusers,"获取人员列表成功!");
    }

}
